package imPro;

import ij.ImagePlus;
import ij.measure.ResultsTable;
import ij.plugin.filter.ParticleAnalyzer;

/**
 *
 * @author Ragnar
 */
public class ImageLabel implements Runnable
{
    private ImPanel ip;
    private Common source;
    
    public ImageLabel(ImPanel ip, Common src)
    {
        this.ip = ip;
        this.source = src;
        Thread t = new Thread(this);
        t.start();
    }
    
    public void run()
    {
        while(true)
        {
            if (this.source.getImageProcessor() != null)
            {
                ResultsTable rt = new ResultsTable();
                ParticleAnalyzer pa = new ParticleAnalyzer(8,            //int options - a flag word created by Oring SHOW_RESULTS, EXCLUDE_EDGE_PARTICLES, etc.
                            65,                                          //int measurements - a flag word created by ORing constants defined in the Measurements interface
                            rt,                                          // rt - a ResultsTable where the measurements will be stored
                            200,                                         //double minSize - the smallest particle size in pixels
                            30000,                                        //double maxSize - the largest particle size in pixels
                            0.3,                                          //minCirc - minimum circularity
                            1);                                             //maxCirc - maximum circularity
                pa.analyze(new ImagePlus("",this.source.getImageProcessor()));
                double area = 0;
                double xpos = 0;
                double ypos = 0;
                try
                {
                    area = rt.getValueAsDouble(0, 0); 
                    xpos = rt.getValueAsDouble(8, 0); 
                    ypos = rt.getValueAsDouble(9, 0);

                }
                catch(Exception ex) {}

                LabeledObject lo = new LabeledObject((int) area, (int) xpos, (int) ypos);
                this.ip.setLabeledObject(lo);
                this.ip.setImage(this.source.getImage());
                System.out.println("Areal: "+area+". X-posisjon: "+xpos+". y-posisjon: "+ypos);
            }
            
            try
            {
                Thread.sleep(50);
            }
            catch (InterruptedException e) {}
        }
    }
}
